/*****************************************************************************
 * Copyright (c) 2007, Ping Identity
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Ping Identity, nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY PING IDENTITY ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
 * NO EVENT SHALL PING IDENTITY BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
#include "functional_test.h"
#include "infocard.h"
#include "context.h"
#include "test_helpers.h"

const unsigned char TOKEN[] =
"<enc:EncryptedData Type=\"http://www.w3.org/2001/04/xmlenc#Element\""
" xmlns:enc=\"http://www.w3.org/2001/04/xmlenc#\">"
"<enc:EncryptionMethod"
" Algorithm=\"http://www.w3.org/2001/04/xmlenc#aes256-cbc\" />"
"<KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"
"<e:EncryptedKey xmlns:e=\"http://www.w3.org/2001/04/xmlenc#\">"
"<e:EncryptionMethod"
" Algorithm=\"http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p\">"
"<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" />"
"</e:EncryptionMethod>"
"<KeyInfo>"
"<o:SecurityTokenReference"
" xmlns:o=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
"<o:KeyIdentifier"
" ValueType=\"http://docs.oasis-open.org/wss/oasis-wss-soap-message-security-1.1#ThumbprintSHA1\""
" EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">"
"OZ7aKRt8wy7HOIy8HBCJ1njPb9o="
"</o:KeyIdentifier>"
"</o:SecurityTokenReference>"
"</KeyInfo>"
"<e:CipherData>"
"<e:CipherValue>"
"nysHAg/HLZV0TuGcxAX+4w0Os0abW97bO1Ze1cNvjl8tA4kaXbDIbkwarXCE0EoHN1D56xbo4wn"
"so8IlLSv/LItuF17J9M4O1WyM8ouVHSZYVnyFSV9NogGri4qXyMnDEiOjKK82WLlrSB3ER7D6L5"
"xV1S9eT2sJu3ts8ssSNhA="
"</e:CipherValue>"
"</e:CipherData>"
"</e:EncryptedKey>"
"</KeyInfo>"
"<enc:CipherData>"
"<enc:CipherValue>"
"+shM/VWKkY+oJPjpkUVcJMfGXfSYcBwus/KFIjx3loXuyLTO8QoijuU1JI0nl1FFCcduIP4Nz2M"
"0MIDyaVRkC2bwL6XC0RZVLmzo0478VSvaQcYDO09tknBOefQ1O1fRgoV/VsH/6VQTNBu590lhYj"
"a5xbINcJPGl3h4/FNEx0T30i+5gdMmBjN7jCWGbeWgIUPj9BjZzrvrSnKHWO90/jTDPnGq828VW"
"sJAi5m+FgFgNRIbuNOtHEWOQeWqLDfGCGIuAhsTDt68mryzGfoOJ137aW7IheV1H9bJFDVJx+w1"
"D9EBqLrauOCCrMEVZOylHEr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_t TOKEN_PROCESSING_TIME = 1186522501;
const xmlChar TOKEN_AUDIENCE[] = "https://test.labs.pingidentity.com/ic/default.aspx";

CuSuite* CuGetFunctionalSuite(void)
{
	CuSuite *suite = CuSuiteNew();
	SUITE_ADD_TEST(suite, test_infocard_decode);
	return suite;
}
void test_infocard_decode(CuTest *tc)
{
	ic_init();
	ic_ctx ctx = ic_ctx_create();
	CuAssertPtrNotNull(tc, ctx);
	ic_ctx_set_logger(ctx, test_logger_printer, NULL);
	ic_ctx_set_attribute_reporting(ctx, test_attribute_printer, NULL);	
	ic_ctx_load_certificate(ctx, "tests/test.pem");
	ic_ctx_load_key(ctx, "tests/test.pem", NULL);
	ic_ctx_set_current_time(ctx, TOKEN_PROCESSING_TIME);
	ic_ctx_set_audience(ctx, TOKEN_AUDIENCE);
	ic_ctx_set_replay_detection(ctx, ic_disable_replay_detection, NULL);
	
	CuAssertTrue(tc, ic_ctx_process_token(ctx, TOKEN, TOKEN_LEN));
	ic_ctx_destroy(ctx);
	ic_free();
}
